﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric
{
	public class EquipExchangeXMLSetting
	{
		public EquipExchangeXMLSetting(XmlHelper xmlHelper)
		{
			this.m_xmlHelper = xmlHelper;
			this.m_lstLinkEleSet = new List<LinkEleItem>();
			this.m_lstExchangeSet = new List<ExchangeSetItem>();
		}

		public void Clear()
		{
			this.m_lstLinkEleSet.Clear();
			this.m_lstExchangeSet.Clear();
			this.m_strPrjName = "";
		}

		public bool Init()
		{
			if (this.m_xmlHelper.SelectNode("Project"))
			{
				this.m_strPrjName = this.m_xmlHelper.GetAttribute("Name");
				if (this.m_xmlHelper.SelectNode(Path.Combine("Project", "LinkEleSet")))
				{
					foreach (XmlElement xmlElement in this.m_xmlHelper.GetElements())
					{
						LinkEleItem linkEleItem = new LinkEleItem();
						linkEleItem.m_lID = xmlElement.GetAttribute("ID").ToInt(0);
						linkEleItem.m_strLinkDoc = xmlElement.GetAttribute("LinkDoc");
						linkEleItem.m_strViewLevel = xmlElement.GetAttribute("ViewLevel");
						linkEleItem.m_lIDCurExchange = xmlElement.GetAttribute("IDCurExchange").ToInt(0);
						this.m_lstLinkEleSet.Add(linkEleItem);
					}
				}
				if (this.m_xmlHelper.SelectNode(Path.Combine("Project", "ExchangeSet")))
				{
					foreach (XmlElement xmlElement2 in this.m_xmlHelper.GetElements())
					{
						ExchangeSetItem exchangeSetItem = new ExchangeSetItem();
						exchangeSetItem.m_strOrigonEquipType = xmlElement2.GetAttribute("OrigonEquipType");
						exchangeSetItem.m_strOrigonEquipName = xmlElement2.GetAttribute("OrigonEquipName");
						exchangeSetItem.m_strExchangeEquipType = xmlElement2.GetAttribute("ExchangeEquipType");
						exchangeSetItem.m_strExchangeEquipName = xmlElement2.GetAttribute("ExchangeEquipName");
						exchangeSetItem.m_strExchangeEquipFilePath = xmlElement2.GetAttribute("ExchangeEquipFilePath");
						exchangeSetItem.m_nEquipType = xmlElement2.GetAttribute("ExchangeEquipEnumType").ToInt(0);
						this.m_lstExchangeSet.Add(exchangeSetItem);
					}
				}
				return true;
			}
			return false;
		}

		public bool Save()
		{
			if (this.m_xmlHelper.SelectNode("Project"))
			{
				this.m_xmlHelper.SetAttribute("Name", this.m_strPrjName);
			}
			if (!this.m_xmlHelper.SelectNode(Path.Combine("Project", "LinkEleSet")))
			{
				this.m_xmlHelper.AddElement("Project", "LinkEleSet");
			}
			if (this.m_xmlHelper.SelectNode(Path.Combine("Project", "LinkEleSet")))
			{
				foreach (LinkEleItem linkEleItem in this.m_lstLinkEleSet)
				{
					bool flag = false;
					XmlElement xmlElement = null;
					foreach (object obj in this.m_xmlHelper.XmlCurNode.ChildNodes)
					{
						XmlElement xmlElement2 = (XmlElement)obj;
						string attribute = xmlElement2.GetAttribute("LinkDoc");
						string attribute2 = xmlElement2.GetAttribute("ViewLevel");
						long num = (long)xmlElement2.GetAttribute("ID").ToInt(0);
						if ((long)linkEleItem.m_lID == num && linkEleItem.m_strLinkDoc == attribute && linkEleItem.m_strViewLevel == attribute2)
						{
							flag = true;
							xmlElement = xmlElement2;
							break;
						}
					}
					if (!flag)
					{
						xmlElement = this.m_xmlHelper.AddElement("Item");
					}
					xmlElement.SetAttribute("ID", linkEleItem.m_lID.ToString());
					xmlElement.SetAttribute("LinkDoc", linkEleItem.m_strLinkDoc);
					xmlElement.SetAttribute("ViewLevel", linkEleItem.m_strViewLevel);
					xmlElement.SetAttribute("IDCurExchange", linkEleItem.m_lIDCurExchange.ToString());
				}
			}
			if (!this.m_xmlHelper.SelectNode(Path.Combine("Project", "ExchangeSet")))
			{
				this.m_xmlHelper.AddElement("Project", "ExchangeSet");
			}
			if (this.m_xmlHelper.SelectNode(Path.Combine("Project", "ExchangeSet")))
			{
				this.m_xmlHelper.XmlCurNode.RemoveAll();
				foreach (ExchangeSetItem exchangeSetItem in this.m_lstExchangeSet)
				{
					foreach (object obj2 in this.m_xmlHelper.XmlCurNode.ChildNodes)
					{
						XmlElement xmlElement3 = (XmlElement)obj2;
						xmlElement3.GetAttribute("OrigonEquipType");
						string attribute3 = xmlElement3.GetAttribute("OrigonEquipName");
						if (exchangeSetItem.m_strOrigonEquipName == attribute3 && exchangeSetItem.m_strOrigonEquipType == attribute3)
						{
							this.m_xmlHelper.XmlCurNode.RemoveChild(xmlElement3);
							break;
						}
					}
					XmlElement xmlElement4 = this.m_xmlHelper.AddElement("Item");
					xmlElement4.SetAttribute("OrigonEquipType", exchangeSetItem.m_strOrigonEquipType);
					xmlElement4.SetAttribute("OrigonEquipName", exchangeSetItem.m_strOrigonEquipName);
					xmlElement4.SetAttribute("ExchangeEquipType", exchangeSetItem.m_strExchangeEquipType);
					xmlElement4.SetAttribute("ExchangeEquipName", exchangeSetItem.m_strExchangeEquipName);
					xmlElement4.SetAttribute("ExchangeEquipFilePath", exchangeSetItem.m_strExchangeEquipFilePath);
					xmlElement4.SetAttribute("ExchangeEquipEnumType", exchangeSetItem.m_nEquipType.ToString());
				}
			}
			this.m_xmlHelper.SaveXml("");
			return true;
		}

		private const string ProjectRoot = "Project";

		private const string ViewLevel = "ViewLevel";

		private const string ExchangeSet = "ExchangeSet";

		private const string LinkEleSet = "LinkEleSet";

		private const string IDCurExchange = "IDCurExchange";

		private const string ID = "ID";

		private const string NameTemp = "Name";

		private const string LinkDoc = "LinkDoc";

		private const string Item = "Item";

		private const string OrigonEquipType = "OrigonEquipType";

		private const string OrigonEquipName = "OrigonEquipName";

		private const string ExchangeEquipType = "ExchangeEquipType";

		private const string ExchangeEquipName = "ExchangeEquipName";

		private const string ExchangeEquipFilePath = "ExchangeEquipFilePath";

		private const string ExchangeEquipEnumType = "ExchangeEquipEnumType";

		public string m_strPrjName;

		public List<LinkEleItem> m_lstLinkEleSet;

		public List<ExchangeSetItem> m_lstExchangeSet;

		public XmlHelper m_xmlHelper;
	}
}
